﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace SoftwareLogistics.SharepointClient.Controls
{
    public class foo: TextBox
    {

    }

    public class GridTextBlock : Grid
    {
        TextBlock _textBlock;

        public enum FormatTypes
        {
            None,
            User,
            Date,
            LinkedRecord,
            Integer,
            Hours
        }

        public GridTextBlock()
        {
            _textBlock = new TextBlock();
            Children.Add(_textBlock);
            FormatType = FormatTypes.None;
        }

        public string FieldName { get; set; }
        public FormatTypes FormatType { get; set; }

        public Dictionary<string, string> Field
        {
            get { return (Dictionary<string, string>)GetValue(FieldProperty); }
            set { SetValue(FieldProperty, value); }
        }

        public static readonly DependencyProperty FieldProperty = DependencyProperty.Register("Field", typeof(Dictionary<string, string>), typeof(GridTextBlock), new PropertyMetadata(null, OnFieldChanged));

        private static void OnFieldChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
        {
            var control = d as GridTextBlock;
            var field = args.NewValue as Dictionary<string, string>;

            if (field.ContainsKey(control.FieldName))
            {
                var contents = field[control.FieldName];
                switch(control.FormatType)
                {
                    case FormatTypes.None:
                        control.Text = contents;
                        break;
                    case FormatTypes.Date:
                        var date = DateTime.Parse(contents);
                        control.Text = date.ToShortDateString();
                        break;
                    case FormatTypes.LinkedRecord:
                        control.Text = contents.Split(';')[1].TrimStart('#');
                        break;
                    case FormatTypes.User:
                        var name = contents.Split(';')[1].TrimStart('#');
                        var parts = name.Split('|');
                        if(parts.Length > 0)
                            control.Text = parts[parts.Length-1];
                        else
                            control.Text = name;

                        break;
                    case FormatTypes.Hours:
                        var hours = Convert.ToDouble(contents);
                        control.Text = string.Format("{0:0.00}", hours);
                        break;
                    case FormatTypes.Integer:
                        var intValue = Convert.ToInt32(contents);
                        control.Text = string.Format("{0:0}", intValue);
                        break;
                }
            
            }
            else
                control.Text = string.Empty;
        }

        public Brush Foreground
        {
            get { return _textBlock.Foreground; }
            set { _textBlock.Foreground = value; }
        }

        public FontWeight FontWeight
        {
            get { return _textBlock.FontWeight; }
            set { _textBlock.FontWeight = value; }
        }

        public FontFamily FontFamily
        {
            get { return _textBlock.FontFamily; }
            set { _textBlock.FontFamily = value; }
        }

        public double FontSize
        {
            get { return _textBlock.FontSize; }
            set {_textBlock.FontSize = value;}
        }

        public string Text
        {
            get { return _textBlock.Text; }
            set { _textBlock.Text = value; }
        }
    }
}
